/**
 * Copyright ©2012-2018 HopeHaiyang Corporation, All Rights Reserved
 * http://www.wuhanhope.com
 */
package hope.platform.data.server.data.img;

import hope.platform.data.dao.UserRepository;
import hope.platform.data.domain.User;
import hope.platform.data.server.data.header.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * class info
 *
 * @author hujia
 *         Create at 2018/7/17 10:00
 */
@Service
public class UserServiceImg implements UserService {

    @Autowired
    private UserRepository repository;

    @Override
    public User addUser(User user) {
        user.setGmtCreate(new Date());
        user.setGmtModified(new Date());
        return repository.save(user);
    }

    @Override
    public User updateUser(User user) {
        User tmp = repository.findByEmail(user.getEmail()).get();
        if (user.getPassword() != null) {
            tmp.setPassword(user.getPassword());
        }
        if (user.getPhone() != null) {
            tmp.setPhone(user.getPhone());
        }
        if (null != user.getRoleName()) {
            tmp.setRoleName(user.getRoleName());
        }
        if (null != user.getLastLoginTime()) {
            tmp.setLastLoginTime(user.getLastLoginTime());
        }
        if (null != user.getGmtModified()) {
            tmp.setGmtModified(user.getGmtModified());
        }
        return repository.save(tmp);
    }

    @Override
    public List<User> getAll() {
        return repository.findAll();
    }

    @Override
    public Optional<User> getByEmail(String email) {
        return repository.findByEmail(email);
    }

    @Override
    public User getByEmailAndPassword(String email, String password) {
        return repository.findByEmailAndPassword(email, password);
    }
}
